#include "AchievementListXmlHandler.h"
#include <QDebug>

AchievementListXmlHandler::AchievementListXmlHandler()
{
}

bool AchievementListXmlHandler::characters(const QString &str)
{
    m_currentText += str;
    return true;
}

bool AchievementListXmlHandler::fatalError(const QXmlParseException &exception)
{
    qDebug("Parse error at line %d, column %d:\n%s", exception.lineNumber(),
                    exception.columnNumber(), exception.message().toStdString().c_str() );

    return false;
}

bool AchievementListXmlHandler::startElement(const QString &/*namespaceURI*/, const QString &/*localName*/,
                  const QString &qName, const QXmlAttributes &attributes)
{
    //qDebug() << "Start: " << qName;

    if (qName == "achievement")
    {
        m_currentAchievement = Achievement();
        m_currentAchievement.setClosed(attributes.value("closed").toInt());
    }


    m_currentText.clear();
    return true;

}

bool AchievementListXmlHandler::endElement(const QString &/*namespaceURI*/, const QString &/*localName*/,
                const QString &qName)
{
    if (qName == "iconClosed")
        m_currentAchievement.setIconUrl(m_currentText);
    else if (qName == "name")
        m_currentAchievement.setName(m_currentText);
    else if (qName == "description")
        m_currentAchievement.setDescription(m_currentText);
    else if (qName == "achievement")
    {
        m_achievementMap.insert(m_currentAchievement.name(),
                                m_currentAchievement);
    }
    else if (qName == "gameName")
        m_currentGameName = m_currentText;

    //qDebug() << "End: " << qName << ": " << m_currentText;

    return true;
}

const QString& AchievementListXmlHandler::gameName() const
{
    return m_currentGameName;
}

QMap<QString, Achievement> AchievementListXmlHandler::achievements() const
{
    return m_achievementMap;
}
